package com.github.microwww.upms.controller;

import com.github.microwww.upms.service.WebUrlService;
import com.github.microwww.upms.vo.WebUrlValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
import java.util.stream.Collectors;

@RestController()
@RequestMapping(value = "/web-url")
public class WebUrlController {

    @Autowired()
    WebUrlService webUrlService;

    @GetMapping(value = "/detail")
    public WebUrlValue.Info detailWebUrl(int id) {
        return this.webUrlService.findById(id).map(WebUrlValue.Info::new).orElseGet(null);
    }

    @GetMapping(value = "/list")
    public List<WebUrlValue.Simple> listWebUrl(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) {
        return this.webUrlService.findAll(page, size).stream().map(WebUrlValue.Simple::new).collect(Collectors.toList());
    }
}
